package com.bosssoft.common.group;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * Copyright (C), 2001-2031, www.bosssof.com.cn
 * FileName: ListValueConstraintValidator.java
 * Author: chen
 * Date: 2024-05-28 19:00
 * Description: 用于初始化及校验。
 * History:
 * Date Author Version Desc
 * 2024-01-01 bosssoft 1.0.0 initialize this file
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValueGroup,Integer> {

    private Set<Integer> set = new HashSet<>();

    /**
     * 初始化方法
     * @param constraintAnnotation
     */
    @Override
    public void initialize(ListValueGroup constraintAnnotation) {

        int[] vals = constraintAnnotation.vals();

        for (int val : vals) {
            set.add(val);
        }

    }

    /**
     * 判断是否效验成功
     * @param value 需要效验的值
     * @param context
     * @return true or false
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {

        //判断是否有包含的值
        boolean contains = set.contains(value);

        return contains;
    }

}
